﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Reflection;
using System.ComponentModel;
using SystemEnum = System.Enum;

namespace Enums
{
    public class EnumManagement
    {
        #region 获取枚举描述
        /// <summary>
        /// 获取枚举描述
        /// </summary>
        /// <param name="enumSubitem">枚举值</param>
        /// <returns>返回枚举描述</returns>
        public static string GetEnumDescription(object enumSubitem)
        {
            enumSubitem = (System.Enum)enumSubitem;
            string strValue = enumSubitem.ToString();

            FieldInfo fieldinfo = enumSubitem.GetType().GetField(strValue);

            if (fieldinfo != null)
            {

                Object[] objs = fieldinfo.GetCustomAttributes(typeof(DescriptionAttribute), false);

                if (objs == null || objs.Length == 0)
                {
                    return strValue;
                }
                else
                {
                    DescriptionAttribute da = (DescriptionAttribute)objs[0];
                    return da.Description;
                }
            }
            else
            {
                return "不限";
            }

        }
        #endregion

        #region 根据枚举的int值获取枚举值的详细说明
        /// <summary>
        /// 根据枚举的int值获取枚举值的详细说明
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="currentValue">枚举的Int值</param>
        /// <returns>枚举值说明</returns>
        public static string GetFieldTextByValue<T>(int currentValue)
        {
            T currentType = GetField<T>(currentValue);
            //return GetFieldText<T>(currentType);
            return GetEnumDescription(currentType);
        }

        /// <summary>
        /// 根据枚举的int值类型转换为枚举类型
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="currentValue">枚举的int值</param>
        /// <returns>枚举类型</returns>
        public static T GetField<T>(int currentValue)
        {
            return (T)SystemEnum.Parse(typeof(T), currentValue.ToString());
        }

        /// <summary>
        /// 获取枚举值的说明
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="enumType">枚举类型值</param>
        /// <returns>枚举值说明</returns>
        public static string GetFieldText<T>(T enumType)
        {
            System.Type currentType = enumType.GetType();
            //获取字段信息   
            FieldInfo[] fields = currentType.GetFields();

            if (fields == null || fields.Length < 1)
            {

                return enumType.ToString();
            }

            for (int i = 0; i < fields.Length; i++)
            {
                FieldInfo field = fields[i];

                //判断名称是否相等   
                if (field.Name != enumType.ToString())
                {
                    continue;
                }

                //反射自定义属性   
                object[] attributes = field.GetCustomAttributes(
                    typeof(DescriptionAttribute), false);

                foreach (Attribute attribute in attributes)
                {
                    //类型转换找到一个Description，用Description作为成员名称   
                    DescriptionAttribute currentAttribute =
                        attribute as DescriptionAttribute;

                    if (currentAttribute != null)
                    {
                        return currentAttribute.Description;
                    }
                }
            }

            //如果没有检测到合适的注释，则用默认名称   
            return enumType.ToString();

        }
        #endregion

        #region 获取枚举值int
        /// <summary>
        /// 获取枚举值int
        /// </summary>
        /// <param name="enumSubitem">枚举值</param>
        /// <returns>返回枚举值</returns>
        public static int GetEnumValue(object enumSubitem)
        {
            return (int)enumSubitem;
        }
        #endregion




        /// <summary>  
    /// 枚举转字典集合  
    /// </summary>  
    /// <typeparam name="T">枚举类名称</typeparam>  
    /// <param name="keyDefault">默认key值</param>  
    /// <param name="valueDefault">默认value值</param>  
    /// <returns>返回生成的字典集合</returns>  
        public static Dictionary<string, object> EnumListDic<T>(string keyDefault, string valueDefault = "")
        {
            Dictionary<string, object> dicEnum = new Dictionary<string, object>();
            Type enumType = typeof(T);
            if (!enumType.IsEnum)
            {
                return dicEnum;
            }
            if (!string.IsNullOrEmpty(keyDefault)) //判断是否添加默认选项  
            {
                dicEnum.Add(keyDefault, valueDefault);
            }
            string[] fieldstrs = Enum.GetNames(enumType); //获取枚举字段数组  
            foreach (var item in fieldstrs)
            {
                string description = string.Empty;
                var field = enumType.GetField(item);
                object[] arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true); //获取属性字段数组  
                if (arr != null && arr.Length > 0)
                {
                    description = ((DescriptionAttribute)arr[0]).Description;   //属性描述  
                }
                else
                {
                    description = item;  //描述不存在取字段名称  
                }
                dicEnum.Add(description, (int)Enum.Parse(enumType, item));  //不用枚举的value值作为字典key值的原因从枚举例子能看出来，其实这边应该判断他的值不存在，默认取字段名称  
            }
            return dicEnum;
        }
    }
}
